//
//  Xpeed.m
//  CocoaSMS
//
//  Created by Sooyong Wang on 09. 01. 31.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Xpeed.h"

@interface Xpeed (private)
- (void) doActions:(NSString *)actionName;
@end

@implementation Xpeed
- (id)init
{
	self = [super init];
	if ( self )
	{
#error Must set Xpeed userif FIRST.
		[self setValue:@"" forKey:@"userid"];
#error Must set Xpeed Password FIRST.
		[self setValue:@"" forKey:@"password"];

		[self setValue:@"" forKey:@"senderPhone"];
		[self setValue:@"" forKey:@"receiverPhone"];
		[self setValue:@"" forKey:@"message"];
		
		[self setValue:[NSNumber numberWithInt:0] forKey:@"remainingSMSCount"];
		
		[self setValue:[NSDictionary dictionaryWithContentsOfFile:
										[[NSBundle mainBundle] pathForResource:@"Xpeed" ofType:@"plist"]]
						forKey:@"scenario"];		
	}
	return self;	
}

- (void) login
{
	[self doActions:@"LOGIN"];
	[self doActions:@"CHECK"];
}

- (void) sendSMS
{
	[self doActions:@"SMS"];
	[self setValue:@"" forKey:@"message"];
	[self doActions:@"CHECK"];
}

- (BOOL)ableToSend
{
	return (remainingSMSCount > 0) && 
	([[self valueForKey:@"messageBytes"] intValue] > 0) &&
	([[self valueForKey:@"messageBytes"] intValue] <= 80);
}

- (int)messageBytes
{
	NSData *data = [message dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingDOSKorean)];
	return [data length];
}

@end

@implementation Xpeed (private)
- (void) doActions:(NSString *)actionName
{
	NSArray *actions = [scenario valueForKey:actionName];
	
	NSEnumerator *e = [actions objectEnumerator];
	NSDictionary *action;
	while ( action = (NSDictionary *)[e nextObject] )
	{
		NSLog(@"ACTION ITEM : %@", [action valueForKey:@"ACTION_NAME"]);
		
		NSURL *url = [NSURL URLWithString:[action valueForKey:@"URL"]];
		NSHTTPURLResponse *response;
		NSError *error;
		NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
		[request setHTTPShouldHandleCookies:YES];
		
		[request setHTTPMethod:[action valueForKey:@"METHOD"]];
		
		NSMutableDictionary *body = [NSMutableDictionary dictionaryWithDictionary:[action valueForKey:@"BODY"]];
		if ( body )
		{
			NSMutableDictionary *body_bind = [action valueForKey:@"BODY_BIND"];
			if ( body_bind )
			{
				NSEnumerator *ke = [body_bind keyEnumerator];
				NSString *key;
				while ( key = (NSString *)[ke nextObject] )
				{
					NSString *body_bind_value = (NSString *)[body_bind valueForKey:key];
					NSString *self_value = [self valueForKey:body_bind_value];
					[body setValue:self_value forKey:key];
				}
			}
			NSString *body_bind_string = [body formatForHTTPUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingDOSKorean)];
			[request setHTTPBody:[body_bind_string dataUsingEncoding:NSASCIIStringEncoding]];
		}
		else
		{
			[request setHTTPBody:nil];
		}
		
		response = nil;
		error = nil;		
		NSData *response_data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
		
		
		NSDictionary *extract = [action valueForKey:@"EXTRACT"];
		NSString *response_string = [[NSString alloc]
																 initWithData:response_data
																 encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingDOSKorean)];
//		NSLog(response_string);
		if ( extract )
		{
			NSArray *gates = [extract valueForKey:@"GATES"];
			NSString *extracted = [response_string extractBetween:[extract valueForKey:@"FROM"]
																												and:[extract valueForKey:@"TO"]
																											gates:gates];
			[self setValue:extracted forKey:[extract valueForKey:@"BIND"]];
		}
	}

}
@end
